package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderDetailVO;
import com.sky.vo.OrderSubmitVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Tag(name = "C端订单相关接口")
@Validated
public class OrderController {

    @Autowired
    private OrdersService orderService;

    /**
     * 用户下单
     *
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @Operation(summary = "用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单,参数：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }


    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @Operation(summary = "订单支付")
    public Result<String> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        // OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        // log.info("生成预支付交易单：{}", orderPaymentVO);
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        return Result.success();
    }

    /**
     * 支付成功，修改订单状态
     *
     * @param outTradeNo
     * @return
     */
    @PutMapping("/paySuccess")
    @Operation(summary = "支付成功")
    public Result<String> paySuccess(@RequestParam String outTradeNo) {
        orderService.paySuccess(outTradeNo);
        return Result.success();
    }

    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("orderDetail/{id}")
    @Operation(summary = "查询订单详情")
    public Result<OrderDetailVO> orderDetail(@PathVariable Long id) {
        log.info("查询订单详情，订单id：{}", id);
        OrderDetailVO orderDetailVO = orderService.getOrderDetail(id);
        return Result.success(orderDetailVO, MessageConstant.GET_ORDER_DETAIL_SUCCESS);
    }

    /**
     * 取消订单
     *
     * @param id
     * @return
     */
    @PutMapping("cancel/{id}")
    @Operation(summary = "取消订单")
    public Result<String> cancel(@PathVariable Long id) {
         orderService.cancelOrder(id);
         return Result.success(MessageConstant.CANCEL_ORDER_SUCCESS);
    }

    /**
     * 历史订单查询
     * @return
     */
    @GetMapping("/historyOrders")
    @Operation(summary = "历史订单查询")
    public Result<PageResult<OrderDetailVO>> historyOrders(@Valid OrdersPageQueryDTO orderPageQueryDTO) {
        PageResult<OrderDetailVO> orderDetailPageResult = orderService.historyOrders(orderPageQueryDTO);
        if (CollectionUtils.isNotEmpty(orderDetailPageResult.getRecords())){
            return Result.success(orderDetailPageResult, MessageConstant.GET_HISTORY_ORDER_SUCCESS);
        }else {
            return Result.success(orderDetailPageResult, MessageConstant.NOT_FOUND_STATUS_ORDER);
        }
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("repetition/{id}")
    @Operation(summary = "再来一单")
    public Result<String> repetition(@PathVariable Long id) {
        orderService.repetition(id);
        return Result.success(MessageConstant.REPETITION_SUCCESS);
    }

    /**
     * 订单催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    @Operation(summary = "订单催单")
    public Result<String> reminder(@PathVariable Long id) {
        orderService.reminder(id);
        return Result.success(MessageConstant.REMINDER_SUCCESS);
    }

}
